package com.as.client.charts;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.chart.yui.SeriesDefY;

public class Chart {


	public Chart(){
		
	}
	public static SeriesDefY[] createSerie(int n, String s){
		SeriesDefY[] sDef; 
		if(n==2){
			sDef = new SeriesDefY[]{ 
					 new SeriesDefY(s+"1", s+"1"),  
	                 new SeriesDefY(s+"2", s+"2")  
			 };
		}
		else{
			sDef = new SeriesDefY[]{ 
					 new SeriesDefY(s+"1", s+"1") 
	                   
			 };
		}
		return sDef;
	}
	
	public static RecordDef createRecord(int n,String s){
		RecordDef rDef; 
		if(n==2){
			 rDef = new RecordDef(  
		                new FieldDef[]{  
		                         new StringFieldDef("time"),  
		                         new FloatFieldDef(s+"1"), 
		                         new FloatFieldDef(s+"2"), 
		                          
	                   }  
		         );  
			
		}
		else{
			 rDef = new RecordDef(  
		                new FieldDef[]{  
		                         new StringFieldDef("time"),  
		                         new FloatFieldDef(s+"1")
		                        
		                          
	                   }  
		         );  
		}
		return rDef;
		
	}
	

	public static ArrayList GetSize(ArrayList<ArrayList<Double>> jitterList){
		
		Iterator<ArrayList<Double>> itList = jitterList.iterator();
		ArrayList lSize = new ArrayList(); 
		while(itList.hasNext()){
			ArrayList l = itList.next(); 
			lSize.add(l.size());
		}
		if(lSize==null){
			return null; 
		}
		else{
			return lSize; 
		}
		
	}
	

	public static Object[][] getData(ArrayList<ArrayList<Double>>metricList, ArrayList<ArrayList<Timestamp>> timeList, ArrayList<Integer> size,int num){
		int j= 0; 
		Iterator<ArrayList<Double>> i = metricList.iterator();
		Iterator<ArrayList<Timestamp>> it = timeList.iterator();
		Object[][] o ;
		if(num==1){
			o = new Object [(int)size.get(0)][2];

			while(i.hasNext()){
				
				ArrayList<Double> aux = i.next();
				ArrayList<Timestamp> auxT = it.next();
				
				Iterator<Double> auxIt = aux.iterator(); 
				Iterator<Timestamp> auxTIt = auxT.iterator();
				while(auxIt.hasNext() ){
					
					Double metric = (Double) auxIt.next();
					Timestamp t = (Timestamp) auxTIt.next();
					o[j][0]= DateTimeFormat.getShortTimeFormat().format(t); 
					o[j][1]= metric;
					j++;
				}
	 		
			}
		}
		// 2 interfaces.........
		else{
			
			if((int)size.get(0)>(int)size.get(1)){
				 o = new Object [(int)size.get(0)][2];
			}
			else{
				 o = new Object [(int)size.get(1)][2];
			}
			
			

			ArrayList<Double> aux3 = i.next(); 
			ArrayList<Double> aux4 = i.next(); 
			ArrayList<Timestamp> aux5 = it.next(); 
			
			Iterator<Double> it3 = aux3.iterator();
			Iterator<Double> it4 = aux4.iterator();
			Iterator<Timestamp> it5 = aux5.iterator(); 
			
			while(it3.hasNext()){
				Double metric1 = it3.next(); 
				Double metric2 = it4.next(); 
				Timestamp t = it5.next(); 
				o[j][0]=DateTimeFormat.getShortTimeFormat().format(t); 
				o[j][1]=metric1; 
				o[j][2]=metric2; 
				j++;
			}
			
			
			
			
		}
		
		
		return o; 
			
	}
	
	
	
	
}

